:- module(_,_,[make,functions]).
:- use_module(library('make/system_extra')).
:- use_module(library(lists),[append/3]).
:- use_module(library(terms),[atom_concat/2]).
:- use_module(library(format), [format/2, format/3]).
:- use_module(library(random), [random/3]).
:- use_module('../SETTINGS').

compile <- [create_passwd, create_ini, create_address] :-
 	(~os = 'Unix' ->
	 Ext = '.cgi',
%	 R = ' >&/dev/null'
	R = ''
	;
	 Ext = '.cpx',
	 R = ''),
	R = '',
	display('Compiling db_generic_client'), 
	display(Ext), nl,
	do([~ciaoc, ' -o db_generic_client', Ext, ' db_generic_client'], 
	   nofail),
	display('Compiling amos_reviewer_access'), 
	display(Ext), nl,
	do([~ciaoc, ' -o amos_reviewer_access', Ext, ' amos_reviewer_access'], 
	   nofail),
	display('Compiling db_maintenance'), 
	display(Ext), nl,
 	do([~ciaoc, ' -o db_maintenance', Ext, ' db_maintenance'], nofail),
	display('Compiling webdb_client'), 
	display(Ext), nl,
 	do([~ciaoc, ' -o webdb_client', Ext, ' webdb_client'], nofail),
	display('Compiling webdb_front'), 
	display(Ext), nl,
 	do([~ciaoc, ' -o webdb_front', Ext, ' webdb_front'], nofail),
	display('Compiling amos_stop'), nl,
 	do([~ciaoc, ' -o amos_stop', ' dbserverstop'], nofail),
	display('Compiling amos_start'), nl,
 	do([~ciaoc, ' -o amos_start', ' startdbserver'], nofail),
	display('Compiling dbserver'), nl, 
 	do([~ciaoc, ' -a actmods/filebased_publish dbserver',R], nofail),
	do(['chgrp ', ~dbserver_group, ' ./dbserver'],nofail),	
	set_gid('./dbserver'),
	set_perms('password.pl', ~passwdperms).

create_passwd <- :-
	del_file_nofail('password.pl'),
	random(0, 10000, N),
	number_codes(N, NC),
	append("password(", NC, Paux),
	append(Paux, ").", P),
	writef(P, 'password.pl').
	
create_ini <- :-
	(~os = 'Win32' ->
	 Ini = 'dbserver.cpx_ini.pl'
	;
	 Ini = 'dbserver_ini.pl'
	),
	writef("% This file is created by lpmake \n", Ini),
	make_term2list(db_os, ~os, DBOS),
	writef(DBOS, append, Ini),
	make_term2list(db_dir, ~dbhome, DBDIR),
	writef(DBDIR, append, Ini),
	make_term2list(db_url, ~dburl, DBURL),
	writef(DBURL, append, Ini),
	make_term2list(db_description, ~dbdesc, DBDESC),
	writef(DBDESC, append, Ini),
	make_term2list(db_location, ~dblocation, DBLOC),
	writef(DBLOC, append, Ini),
	make_term2list(db_data, ~dbdata, DBDATA),
	writef(DBDATA, append, Ini),
	make_term2list(db_types, ~dbtypes, DBTYPES),
	writef(DBTYPES, append, Ini),
	make_term2list(db_templates, ~dbtemplates, DBTEMPLATES),
	writef(DBTEMPLATES, append, Ini),
	make_term2list(db_css, ~db_css, CSS),
	writef(CSS, append, Ini),
	make_term2list(reviewer_email, ~reviewer_email, AMOS_EMAIL),
	writef(AMOS_EMAIL, append, Ini).
	      
make_term2list(Functor, Arg, LT) :- % Gets both arguments as atoms
	atom_codes(Functor, LF),
	atom_codes(Arg, LA),
	append(LF, "('", LFC),
	append(LFC, LA, LFCA),
	append(LFCA, "') . \n", LT).
	
	
create_address <- :-
	atom_codes(~dbadd, ACaux),
	append([0''|ACaux],"/' .", AC),
	atom_concat(~dbhome, '/.addr', Dir),
	writef(AC, Dir),
	atom_concat(~dbadmin, '/.addr', AdminDir),
	writef(AC, AdminDir) .

install <- [maintenance] :-
	(~os = 'Unix' ->
	 move_files(['webdb_client.cgi', 'webdb_front.cgi'], ~dbhome),
	 move_files(['db_generic_client.cgi', 'amos_reviewer_access.cgi', 
	             'db_maintenance.cgi'], ~dbadmin),
	 do(['/bin/cp dbserver dbserver_ini.pl ', ~dbadmin],nofail)
	;
	 move_files(['webdb_client.cpx', 'webdb_front.cpx'], ~dbhome),
	 move_files(['webdb_client.bat', 'webdb_front.bat'], ~dbhome),
	 move_files(['db_generic_client.cpx', 'amos_reviewer_access.cpx', 
	             'db_maintenance.cpx'], ~dbadmin),
	 move_files(['db_generic_client.bat', 'amos_reviewer_access.bat', 
	             'db_maintenance.bat'], ~dbadmin),
	 move_files(['dbserver.bat', 'dbserver.cpx', 'dbserver.cpx_ini.pl', ' maintenance.html'], ~dbadmin) 
	),
	move_files([amos_start, amos_stop], ~dbadmin).

maintenance <- :-
	del_file_nofail('./.addr'),
	del_file_nofail('./.lock'),
	atom_codes(~dbadd, ACaux),
	append([0''|ACaux],"/' .", AC),
	writef(AC, './.addr'),
	(~os = 'Win32' ->
	 Dbserver = './dbserver.bat',
	 do(['chgrp -R ',~dbserver_group,' ', 'dbserver.cpx'], nofail)
	;
	 Dbserver = './dbserver'),
	do(['chgrp -R ',~dbserver_group,' ',Dbserver], nofail),
	do([Dbserver], nofail),
	del_file_nofail('./.addr'),
	display('Additional HTML pages created'), nl .

clean <- :-
	delete_files(~ls('*.po|*.itf|tmp*|*~|password.pl|.addr|.lock|amos_start|amos_stop')),
	newline(NL),
	display(NL), nl,
	display('Deleted files *.po, *.itf, tmp*, *~, password.pl, .addr, .lock'),nl,
	display(NL), nl.
distclean <- [clean] :-
	newline(NL),
	display(NL), nl,
	delete_files(~ls('*.cgi|*.cpx|*.bat|dbserver|*_ini.pl')),
	display('Deleted files *.cgi, dbserver, *_ini.pl'), 
	nl, display(NL), nl.
	
set_gid(File) :-
	file_property(File,mode(Permissions)),
	NewPermissions is Permissions \/ 0o2000,
	chmod(File, NewPermissions).
	
newline('------------------------------------------------------------------'). 
